package com.xjd.edu.admin.service.sys.remote;

import com.xjd.edu.admin.service.RemoteUserService;
import com.xjd.edu.admin.service.sys.ISysConfigService;
import com.xjd.edu.admin.service.sys.ISysPermissionService;
import com.xjd.edu.admin.service.sys.ISysUserService;
import com.xjd.edu.common.admin.domain.SysUser;
import com.xjd.edu.common.admin.model.LoginUser;
import com.xjd.edu.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/com.xjd.edu.admin.service.remoteUserService")
public class RemoteSysUserServiceImpl implements RemoteUserService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysPermissionService permissionService;

    @Autowired
    private ISysConfigService configService;

    @Override
    public LoginUser getUserInfo(String username, String source) {
        SysUser sysUser = userService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            //return R.fail("用户名或密码错误");
            log.info("RemoteSysUserServiceImpl.getUserInfo,sysUser:{} 用户名或密码错误", username);
            return null;
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(sysUser);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser);
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSysUser(sysUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        return sysUserVo;
    }

    @Override
    public LoginUser getUserInfoByName(String username, String source) {
        SysUser sysUser = userService.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            //return R.fail("用户名或密码错误");
            log.info("RemoteSysUserServiceImpl.getUserInfo,sysUser:{} 用户名或密码错误", username);
            return null;
        }
        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(sysUser);
        return loginUser;
    }

    @Override
    public Boolean registerUserInfo(SysUser sysUser, String source) {
        /*String username = sysUser.getUserName();
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return R.fail("当前系统没有开启注册功能！");
        }
        if (!userService.checkUserNameUnique(sysUser)) {
            return R.fail("保存用户'" + username + "'失败，注册账号已存在");
        }*/
        return userService.registerUser(sysUser);
    }

    @Override
    public Boolean recordUserLogin(SysUser sysUser, String source) {
        return userService.updateUserProfile(sysUser);
    }
}
